﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;
using DevComponents.DotNetBar;

namespace Waiter.View
{
    public partial class FrmUser : DevComponents.DotNetBar.Office2007Form
    {
        public FrmUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Setting thong tin header
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void headerItem1_Load(object sender, EventArgs e)
        {
            this.headerItem1.init("TÀI KHOẢN", false);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.btnBack_Click", "", "");
            this.Close();
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.FrmUser_Load", "", "");
            loadingFlag = CommonConstants.FLAG_ON;
            fsLoadCmbType();
            fsLoadDgrStaff();
            loadingFlag = CommonConstants.FLAG_OFF;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrStaff, 0, 1);
        }

        private void fsLoadDgrStaff()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.fsLoadDgrStaff", "", "");
            this.dgrStaff.Rows.Clear();

            StaffCtrl staffCtrl = new StaffCtrl();
            UserCtrl userCtrl = new UserCtrl();
            List<STAFF> staffList = staffCtrl.fsGetAllStaffs();
            if (staffList == null || staffList.Count == 0)
            {
                return;
            }

            this.dgrStaff.Rows.Add(staffList.Count);
            for (int i = 0; i < staffList.Count; i++)
            {
                this.dgrStaff.Rows[i].Cells["colStaffCode"].Value = staffList[i].CODE;
                this.dgrStaff.Rows[i].Cells["colStaffName"].Value = staffList[i].NAME;
                this.dgrStaff.Rows[i].Cells["colStaffPhone"].Value = staffList[i].PHONE_NUMBER;
                this.dgrStaff.Rows[i].Cells["colStaffAddress"].Value = staffList[i].ADDRESS;
                this.dgrStaff.Rows[i].Cells["colStaffSex"].Value = staffList[i].SEX;

                int userType = userCtrl.fsGetUserType(staffList[i].CODE);
                if (userType == (int)UserType.ADMIN)
                {
                    this.dgrStaff.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                }
            }
        }

        bool loadingFlag = CommonConstants.FLAG_OFF;
        private void fsLoadStaffInfo()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.fsLoadStaffInfo", "", "");

            this.txtStaffName.Text = CommonUtils.fsToString(this.dgrStaff.CurrentRow.Cells["colStaffName"].Value.ToString());
            this.txtStaffPhone.Text = CommonUtils.fsToString(this.dgrStaff.CurrentRow.Cells["colStaffPhone"].Value.ToString());
            this.txtStaffAddress.Text = CommonUtils.fsToString(this.dgrStaff.CurrentRow.Cells["colStaffAddress"].Value.ToString());
            string sex = CommonUtils.fsToString(this.dgrStaff.CurrentRow.Cells["colStaffSex"].Value.ToString());
            this.txtStaffSex.Text = sex == "True" ? "Nam" : "Nữ";
        }

        private void dgrStaff_SelectionChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.dgrStaff_SelectionChanged", "", "");
            if (loadingFlag)
            {
                return;
            }
            fsLoadStaffInfo();
            fsLoadAccountInfo();
        }

        private void fsClearAccountInfo()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.fsClearAccountInfo", "", "");
            this.txtLoginName.Text = string.Empty;
            this.txtPassword.Text = string.Empty;
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbType, 0);
        }

        private void fsLoadAccountInfo()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.fsLoadAccountInfo", "", "");
            fsClearAccountInfo();

            if (this.dgrStaff.Rows.Count == 0)
            {
                return;
            }

            UserCtrl userCtrl = new UserCtrl();
            string staffCode = this.dgrStaff.CurrentRow.Cells["colStaffCode"].Value.ToString();
            USER item = userCtrl.fsGetUserByStaff(staffCode);
            if (item == null)
            {
                return;
            }

            this.txtLoginName.Text = item.USER_NAME;
            this.txtPassword.Text = item.PASS_WORD;
            CommonCtrl.fsCmbSetSelectedValue(this.cmbType, item.CODE_TYPE.ToString());
        }

        private void fsLoadCmbType()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.fsLoadCmbType", "", "");
            ComboboxItem item1 = new ComboboxItem();
            item1.Value = ((int)UserType.USER).ToString();
            item1.Text = "Tài khoản thường";

            ComboboxItem item2 = new ComboboxItem();
            item2.Value = ((int)UserType.ADMIN).ToString();
            item2.Text = "Tài khoản quản lý";

            List<ComboboxItem> items = new List<ComboboxItem>();
            items.Add(item1);
            items.Add(item2);

            this.cmbType.DataSource = items;
            this.cmbType.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbType.ValueMember = CommonConstants.CMB_VALUE;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.btnSave_Click", "", "");
            if (this.dgrStaff.Rows.Count == 0)
            {
                return;
            }

            string loginName = this.txtLoginName.Text.Trim();
            if (loginName == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập tên đăng nhập!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtLoginName.Focus();
                return;
            }

            string password = this.txtPassword.Text.Trim();
            if (password == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập mật khẩu!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtPassword.Focus();
                return;
            }

            int userType = Convert.ToInt32(CommonCtrl.fsCmbGetSelectedValue(this.cmbType));
            UserCtrl userCtrl = new UserCtrl();
            string staffCode = this.dgrStaff.CurrentRow.Cells["colStaffCode"].Value.ToString();
            ResultStatus rs = userCtrl.fsInsertOrUpdateUser(staffCode,
                userType,
                loginName,
                password);

            if (rs != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã thực hiện thành công!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            // update grid info
            if (userType == (int)UserType.ADMIN)
            {
                this.dgrStaff.CurrentRow.DefaultCellStyle.ForeColor = Color.Red;
            }
            else
            {
                this.dgrStaff.CurrentRow.DefaultCellStyle.ForeColor = SystemColors.ControlText;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.btnDelete_Click", "", "");
            if (this.dgrStaff.Rows.Count == 0)
            {
                return;
            }

            UserCtrl userCtrl = new UserCtrl();
            int idx = this.dgrStaff.CurrentRow.Index;
            string staffCode = this.dgrStaff.Rows[idx].Cells["colStaffCode"].Value.ToString();
            ResultStatus rs = userCtrl.fsDeleteUserByStaffCode(staffCode);
            if (rs != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //DevComponents.DotNetBar.MessageBoxEx.Show("Đã thực hiện thành công!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
            fsClearAccountInfo();
            this.dgrStaff.CurrentRow.DefaultCellStyle.ForeColor = SystemColors.ControlText;
        }

        private void headerItem1_Load_1(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmUser.headerItem1_Load_1", "", "");
            this.headerItem1.init("TÀI KHOẢN", false);
        }
    }
}


